package com.party.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;

import com.party.domain.Discuss;
/**
 * 
 * @author Caizhf
 * @date 2017年5月14日上午10:24:45
 * @version v.0.1
 * @email 1115054416@qq.com 
 *
 * <p>Description: 评论</p>
 *
 */
public interface DiscussMapper {
	
	@Insert("INSERT INTO discuss(discuss_comment_content,discuss_comment_time,user_id,product_id)"
			+ "VALUES(#{discuss_comment_content},#{discuss_comment_time},#{user.id},#{product.pid})")
	@SelectKey(before=false,keyProperty="did",resultType=Integer.class, statement = { "SELECT LAST_INSERT_ID()" })
	int add(Discuss discuss);
	
	
	@Delete("DELETE FROM discuss WHERE did=#{discuss_id}")
	int deleteById(int discuss_id);
	
	//在DiscussMapper.xml中声明，可以做更好的对比
	Discuss findById(int discuss_id);
	
	/**
	 * 查询某款商品的所有评论（分页）
	 * @param product_id 商品
	 * @param pageStart  开始记录
	 * @param pageSize	  需要查询的记录数
	 * @return
	 */
	@Select("SELECT * FROM product p,discuss d WHERE d.product_id=p.pid "
			+ "AND p.pid=#{product_id} LIMIT #{pageStart},#{pageSize}")
	@ResultMap("com.party.mapper.DiscussMapper.DiscussBaseMapped")
	List<Discuss> findByProductId(@Param("product_id")int product_id,@Param("pageStart")int pageStart,@Param("pageSize") int pageSize);
	
	
	//查询某个用户的所有评论
	@Select("SELECT * FROM discuss d,user u WHERE d.user_id=u.id AND u.id=#{user_id}")
	@ResultMap("com.party.mapper.DiscussMapper.DiscussBaseMapped")
	List<Discuss> findByUserId(int user_id);

	
	@Update("UPDATE discuss SET discuss_comment_content=#{discuss_comment_content},discuss_comment_time=#{discuss_comment_time} WHERE did=#{did}")
	int update(Discuss discuss);
	
	//查看某个商品下的所有评价总数
	@Select("SELECT COUNT(*) FROM product p,discuss d WHERE d.product_id=p.pid AND p.pid=#{product_id}")
	int findProductDiscussTotleRecord(int product_id);
}
